using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace DarkNeuron.Services.Input
{
	public class GameInput : Microsoft.Xna.Framework.GameComponent
	{
		public static Game Game { get; private set; }
		private static ButtonManager buttonManager;
		public static ButtonManager ButtonManager { get { return buttonManager; } }
		public static bool Initialized { get; private set; }
// #if !xbox
		// PC Keyboard
		protected static KeyboardState currentKeyboardState;
		public static KeyboardState Keyboard { get { return currentKeyboardState; } }
        protected static KeyboardState previousKeyboardState;
		public static KeyboardState KeyboardPrevious { get { return previousKeyboardState; } }
		// PC Mouse
		protected static MouseState currentMouseState;
		public static MouseState Mouse { get { return currentMouseState; } }
		protected static MouseState previousMouseState;
		public static MouseState MousePrevious { get { return previousMouseState; } }
		protected static int mouseScroll;
		protected static int mouseScrollPrevious;
		public static int MouseScrollWeelDelta { get { return mouseScroll - mouseScrollPrevious; } }
		public static bool MouseVisable
		{
			get { return Game.IsMouseVisible; }
			set { Game.IsMouseVisible = value; }
		}
// #else
		// XBox Keyboards
		protected static KeyboardState[] currentKeyboardXBoxState;
		public static KeyboardState KeyboardOne { get { return currentKeyboardXBoxState[0]; } }
		public static KeyboardState KeyboardTwo { get { return currentKeyboardXBoxState[1]; } }
		public static KeyboardState KeyboardThree { get { return currentKeyboardXBoxState[2]; } }
		public static KeyboardState KeyboardFour { get { return currentKeyboardXBoxState[3]; } }
		public static KeyboardState[] KeyboardsXBox { get { return currentKeyboardXBoxState; } }
		protected static KeyboardState[] previousKeyboardXBoxState;
		public static KeyboardState KeyboardPreviousOne { get { return previousKeyboardXBoxState[0]; } }
		public static KeyboardState KeyboardPreviousTwo { get { return previousKeyboardXBoxState[1]; } }
		public static KeyboardState KeyboardPreviousThree { get { return previousKeyboardXBoxState[2]; } }
		public static KeyboardState KeyboardPreviousFour { get { return previousKeyboardXBoxState[3]; } }
		public static KeyboardState[] KeyboardsPreviousXBox { get { return previousKeyboardXBoxState; } }
		// XBox Gamepads
		protected static GamePadState[] currentGamePadsState;
		public static GamePadState GamePadOne { get { return currentGamePadsState[0]; } }
		public static GamePadState GamePadTwo { get { return currentGamePadsState[1]; } }
		public static GamePadState GamePadThree { get { return currentGamePadsState[2]; } }
		public static GamePadState GamePadFour { get { return currentGamePadsState[3]; } }
		public static GamePadState[] GamePads { get { return currentGamePadsState; } }
		protected static GamePadState[] previousGamePadsState;
		public static GamePadState GamePadPreviousOne { get { return previousGamePadsState[0]; } }
		public static GamePadState GamePadPreviousTwo { get { return previousGamePadsState[1]; } }
		public static GamePadState GamePadPreviousThree { get { return previousGamePadsState[2]; } }
		public static GamePadState GamePadPreviousFour { get { return previousGamePadsState[3]; } }
		public static GamePadState[] GamePadsPreviousState { get { return previousGamePadsState; } }
		protected static Vector2[] currentGamePadVibretorsState;
		public static Vector2 GamePadVibratorOne { get { return currentGamePadVibretorsState[0]; } set { currentGamePadVibretorsState[0] = value; } }
		public static Vector2 GamePadVibratorTwo { get { return currentGamePadVibretorsState[1]; } set { currentGamePadVibretorsState[1] = value; } }
		public static Vector2 GamePadVibratorThree { get { return currentGamePadVibretorsState[2]; } set { currentGamePadVibretorsState[2] = value; } }
		public static Vector2 GamePadVibratorFour { get { return currentGamePadVibretorsState[3]; } set { currentGamePadVibretorsState[3] = value; } }
		public static Vector2[] GamePadVibrators { get { return currentGamePadVibretorsState; } }
		protected static Vector2[] previousGamePadVibretorsStatus;
		public static Vector2 GamePadPreviousVibratorOne { get { return previousGamePadVibretorsStatus[0]; } }
		public static Vector2 GamePadPreviousVibratorTwo { get { return previousGamePadVibretorsStatus[1]; } }
		public static Vector2 GamePadPreviousVibratorThree { get { return previousGamePadVibretorsStatus[2]; } }
		public static Vector2 GamePadPreviousVibratorFour { get { return previousGamePadVibretorsStatus[3]; } }
		public static Vector2[] GamePadPreviousVibratorsState { get { return previousGamePadVibretorsStatus; } }
// #endif


		public GameInput(Game game)
			: base(game)
		{
			Enabled = true;
			Game = game;
			
			if (!Initialized) 
				Initialize();
		}


		public override void Initialize()
		{
			// Adds ButtonManager to components list
			buttonManager = new ButtonManager(Game);
			Game.Components.Add(buttonManager);
			// Initializes variables
// #if xbox
			currentKeyboardXBoxState = new KeyboardState[4];
			previousKeyboardXBoxState = new KeyboardState[4];
			currentGamePadsState = new GamePadState[4];
			previousGamePadsState = new GamePadState[4];
			currentGamePadVibretorsState = new Vector2[4];
			previousGamePadVibretorsStatus = new Vector2[4];
// #endif

		
			base.Initialize();
			Initialized = true;
		}

		public override void Update(GameTime gameTime)
		{
// #if !xbox
			// Updates PC Keyboard
			previousKeyboardState = currentKeyboardState;
			currentKeyboardState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
			// Updates PC Mouse
			previousMouseState = currentMouseState;
			currentMouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();
			mouseScrollPrevious = mouseScroll;
			mouseScroll = currentMouseState.ScrollWheelValue;
// #else
			// Updates XBox Keyboards & XBox GamePads
			for (int playerIndex = 0; playerIndex < 4; playerIndex++)
			{
				// XBox Keyboards
				previousKeyboardXBoxState[playerIndex] = currentKeyboardXBoxState[playerIndex];
				currentKeyboardXBoxState[playerIndex] = Microsoft.Xna.Framework.Input.Keyboard.GetState((PlayerIndex)playerIndex);
				// XBox GamePads
				previousGamePadsState[playerIndex] = currentGamePadsState[playerIndex];
				currentGamePadsState[playerIndex] = Microsoft.Xna.Framework.Input.GamePad.GetState((PlayerIndex)playerIndex);
				// XBox GamePad Vibrators
				previousGamePadVibretorsStatus[playerIndex] = currentGamePadVibretorsState[playerIndex];
				Microsoft.Xna.Framework.Input.GamePad.SetVibration(
					(PlayerIndex)playerIndex, 
					currentGamePadVibretorsState[playerIndex].X, 
					currentGamePadVibretorsState[playerIndex].Y);
			}
// #endif

			base.Update(gameTime);
		}
	}
}